<template>
  <div>
    <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad" :error.sync="error"
      error-text="请求失败，点击重新加载">
      <van-cell v-for="item in list" :key="item.art_id" :title="item.title" />
    </van-list>
  </div>
</template>

<script>
import { getSearchResult } from '@/api/search.js'
export default {
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1,
      per_page: 10,
      error: false
    }
  },
  props: {
    value: {
      type: String,
      required: true
    }
  },
  methods: {
    async onLoad () {
      // 发请求获取数据
      try {
        const { data: res } = await getSearchResult({
          page: this.page,
          per_page: this.per_page,
          q: this.value
        })
        this.list.push(...res.data.results)
        // 当前这次状态加载结束
        this.loading = false
        if (Math.random() > 0.5) {
          console.lg(123)
        }
        // 数据全部加载完毕
        if (this.list.length === res.data.total_count) {
          this.finished = true
        } else {
          this.page++
        }
      } catch (err) {
        // 关闭loading
        this.loading = false
        this.error = true
      }
      // 加载状态结束
      // this.loading = false
    }
  }
}
</script>

<style>
</style>
